﻿global using Microsoft.AspNetCore.Authorization;
global using MyBlog.IService;
global using System;
global using System.Threading.Tasks;
global using System.Collections.Generic;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;

using MyBlog.Model;
using MyBlog.Model.DTO;

using SqlSugar;


namespace MyBlog.WebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class BlogNewsController : ControllerBase
    {
        private readonly IBlogNewsService blogNewsService;

        public BlogNewsController(IBlogNewsService blogNewsService)
        {
            this.blogNewsService = blogNewsService;
        }
        [HttpGet("BlogNews")]
        public async Task<ActionResult<ApiResult>> GetBlogNews()
        {
            var id = Convert.ToInt32(this.User.FindFirst("Id").Value);

            var data = await blogNewsService.QueryAsync(c => c.WriterId == id);
            if (data == null) return ApiResultHelper.Error("没有更多的文章");
            return ApiResultHelper.Success(data);
        }
        [HttpPost("Create")]
        public async Task<ActionResult<ApiResult>> Create(string title,string content,int typeid)
        {
            //数据验证
            BlogNews blogNews = new()
            {
                BrowseCount = 0,
                Content = content,
                LikeCount = 0,
                Time = DateTime.Now,
                Title = title,
                TypeId = typeid,
                WriterId = Convert.ToInt32(this.User.FindFirst("Id").Value)
            };
            bool b=await blogNewsService.CreateAsync(blogNews);
            if (!b) return ApiResultHelper.Error("添加失败，服务器发生错误");
            return ApiResultHelper.Success(blogNews);
        }
        [HttpDelete("Delete")]
        public async Task<ActionResult<ApiResult>> Delete(int id)
        {
            bool b = await blogNewsService.DeleteAsync(id);
            if (!b) return ApiResultHelper.Error("删除失败");
            return ApiResultHelper.Success(b);
        }
        [HttpPut("Edit")]
        public async Task<ActionResult<ApiResult>> Edit(int id, string title, string content, int typeid)
        {
            var blogNews = await blogNewsService.FindAsync(id);
            if(blogNews==null)return ApiResultHelper.Error("没有找到该文章");

            blogNews.Title = title;
            blogNews.Content = content;
            blogNews.TypeId = typeid;
            bool b=await blogNewsService.EditAsync(blogNews);

            if (!b) return ApiResultHelper.Error("修改失败");
            return ApiResultHelper.Success(blogNews);
        }
        [HttpGet("GetBlogNewsPage")]
        public async Task<ActionResult<ApiResult>> GetBlogNewsPage([FromServices]IMapper mapper, int page,int size)
        {
            RefAsync<int> total = 0;
            var blogNews = await blogNewsService.QueryAsync(page, size, total);
            try
            {
                var blogNewsDTOs = mapper.Map<List<BlogNewsDTO>>(blogNews);
                return ApiResultHelper.Success(blogNewsDTOs, total);
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("AutoMapper映射异常");
            }
            

        }
    }
}
